/**
 * created by guyi 2018.1.22
 */
var express = require('express');
var router = express.Router();
var superagent = require('superagent');
require('superagent-proxy')(superagent);
var redis = require("redis");
var cheerio = require('cheerio');
const UA_List = require('./UA_List');
const client = redis.createClient(6379, "127.0.0.1");

var moment = require('moment');

async function main() {
    var pageNum = randomNum(15);
    patch(pageNum);
}

async function patch(pageNum) {
    var header = UA_List.get()
    const patchUrl = 'http://www.xicidaili.com/nt/' + pageNum
    console.log(header);
    superagent.get(patchUrl)
    .set({'User-Agent':header})
        .end((err, data) => {
            if (err) {
                res.send(err);
            } else {
                var $ = cheerio.load(data.text);
                $('#ip_list tr.odd').each(function(index, element) {
                    var ip = $(element).children().eq(1).text();
                    var port = $(element).children().eq(2).text();
                    var type = $(element).children().eq(5).text();
                    let item = type.toLowerCase() + '://' + ip + ':' + port;
                    let speed = $(element).children().eq(6).children('.bar').attr().title; // string 带单位秒
                    let mathspeed = speed.substring(0, speed.length - 1)
                    // console.log(mathspeed);
                    if (mathspeed<1) {
                        client.sadd('proxy', item, (err, res) => {
                            if (err) {
                                console.log(err);
                            } else {
                                console.log('sucessful update proxy list');
                            }
                        });
                    }
                });
            } 
        });
}
function randomNum(Max, Min=0) {
    var Range = Max - Min;   
    var Rand = Math.random();   
    return(Min + Math.round(Rand * Range));  
}

module.exports = {main}